// hyb_yl/czhuanjiasubpages/pages/questions/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    progress: '0', //进度条
    pagenum: 1, //第几题
    pro_grap: [{
        question: '本次患病多久了？',
        ansers: ['一周内', '一月内', '半年内', '大于半年']
      },
      {
        question: '本次咨询的疾病名称或症状？',
        questi: '例糖尿病，白癜风或头疼',
        one_items: []
      },
      {
        question: '本次咨询的疾病去医院就诊过吗？',
        questi: '曾经的就诊经历，是给医生的重要参考',
        ansers: ['就诊过', '没就诊过'],
        one_items: [],
        input1: '',
        input2: ''
      },
      {
        question: '有可以反映病情的患处照片吗？',
        ansers: ['有', '暂时没有'],
        img: []
      },
      {
        question: '当前是否有正在使用的药物？',
        ansers: ['有', '没有'],
        img:[]
      },
      {
        question: '是否有过手术，放化疗等重大疾病治疗经历及慢性病史',
        questi: '患者但是每年的萨满的拿手的拿手的',
        ansers: ['有', '没有'],
        one_items: []
      },
      {
        question: '是否有过敏史？',
        questi: '如有变化请补充',
        ansers: ['有', '没有'],
        one_items: []
      },
      {
        question: '希望获得医生什么帮助',
        questi: '可向医生询问是否可去门诊就诊，如何控制病情，是否需要手术等',
        textarea:""
        // ansers: ['有', '没有']
      }
    ], //题目   
    data_arr: [],
    items: [],
    input1: '',
    input2: '',
    img: [], //问题二图片
    img2: [],
    // onedown_key: true,
    one_mask_key: true,
    one_textarea: '',
    //问题二
    addinfo_key: true,
    addinfos_key: true,
    threedown_key: true, //问题三下拉
    fourdown_key: true,
    fivedown_key: true,
    five_mask_key: true,
    five_items: [],
    five_textarea: '',
    sixdown_key: true,
    six_items: [],
    six_mask_key: true,
    six_textarea: '',
    doc_advi: '', //医生建议
    trandata: [],
    cindex:0
  },

  //回答问题 
  dianbtn: function(e) {
    this.setData({
      ans_index: e.currentTarget.dataset.cindex
    })

    let index = e.currentTarget.dataset.index
    let cindex = e.currentTarget.dataset.cindex
    this.setData({
      cindex
    })
    // let tranobj = {
    //   quest: this.data.pro_grap[index].question,
    //   anser: this.data.pro_grap[index].ansers[cindex]
    // }

    // this.data.trandata.push(tranobj)
    // console.log(this.data.trandata)

    if (index == 2 && cindex == 0) {
      this.setData({
        addinfo_key: false
      })
    } else if (index == 2 && cindex != 0) {
      this.setData({
        addinfo_key: true
      })
    } else if (index == 3 && cindex == 0) {
      this.setData({
        threedown_key: false
      })
    } else if (index == 3 && cindex != 0) {
      this.setData({
        threedown_key: true
      })
    } else if (index == 4 && cindex == 0) {
      this.setData({
        fourdown_key: false
      })
    } else if (index == 4 && cindex != 0) {
      this.setData({
        fourdown_key: true
      })
    } else if (index == 5 && cindex == 0) {
      this.setData({
        fivedown_key: false
      })
    } else if (index == 5 && cindex != 0) {
      this.setData({
        fivedown_key: true
      })
    } else if (index == 6 && cindex == 0) {
      this.setData({
        sixdown_key: false
      })
    } else if (index == 6 && cindex != 0) {
      this.setData({
        sixdown_key: true
      })
    }
  },
  // 上一题
  btn_prev: function() {
    this.data.pagenum -= 1
    if (this.data.pagenum <= 0) {
      return false
    } else if (this.data.pagenum != 3){
      this.setData({
        addinfo_key: true
      })
    }
    
    let index = parseFloat(this.data.pagenum)-1
    this.data.trandata.splice(index,1)
    this.setData({
      trandata: this.data.trandata
    })
    console.log('上一题病例数组', this.data.trandata)
    //进度条
    let progress = parseFloat(this.data.progress)
    progress -= 10

    this.setData({
      pagenum: this.data.pagenum,
      progress: progress,
      onedown_key: true,
      threedown_key: true,
      fourdown_key: true,
      fivedown_key: true,
      sixdown_key: true
    })
  },
  //下一题
  btn_next: function() {
    var zid = this.data.zid
    var name = this.data.name
    var keywords = this.data.keywords
    let pagenum = parseFloat(this.data.pagenum)
    console.log('本题数',pagenum)
    // console.log('本题数', this.data.pro_grap[pagenum - 1].ansers)
    if (this.data.pro_grap[pagenum - 1].ansers) {
      if (this.data.ans_index == undefined || this.data.ans_index == -1) {
        wx.showToast({
          title: '请选择',
          icon: "none"
        })
        return false
      } else {
        this.getarr() //获取数组
        this.getnext()
      }
    } else if (this.data.pro_grap[pagenum - 1].ansers == undefined) {
      this.getarr() //获取数组
      if (pagenum == 2 && this.data.pro_grap[1].one_items.length == 0){
        wx.showToast({
          title: '请补充信息',
          icon:'none'
        })
        return false
      
      }
      this.getnext()
    }

  },
  //获取数组
  getarr:function(){
    let pagenum = parseFloat(this.data.pagenum)
    if (pagenum !== 2 && pagenum !== 8){
      console.log(!222222)
      let index = pagenum - 1 //数组索引
      let cindex = this.data.cindex //问题索引
      let tranobj = {
        quest: this.data.pro_grap[index].question,
        anser: this.data.pro_grap[index].ansers[cindex],
        one_items: this.data.pro_grap[index].one_items,
        img: this.data.pro_grap[index].img
      }
      this.data.trandata.push(tranobj)
      console.log('下一题病例数组', this.data.trandata)
    } else if (pagenum == 2 && this.data.pro_grap[1].one_items.length>0){
      let index = pagenum - 1 //数组索引
      let quest= this.data.pro_grap[index].question
      let contarr = this.data.pro_grap[1].one_items
      let obj={
        quest: this.data.pro_grap[index].question,
        contarr: this.data.pro_grap[1].one_items
      }
      this.data.trandata.push(obj)
      console.log('contarr', this.data.trandata)  
    } else if (pagenum == 8 && this.data.pro_grap[7].textarea){
      let quest = this.data.pro_grap[index].question
      let textarea= this.data.pro_grap[7].one_items
      let obj = {
        quest: this.data.pro_grap[index].question,
        textarea: this.data.pro_grap[7].textarea
      }
      this.data.trandata.push(obj)
      console.log('contarr', this.data.trandata) 
    }
  },
  
  getnext: function() {
    var zid = this.data.zid
    var name = this.data.name
    var keywords = this.data.keywords
    let pro_len = this.data.pro_grap.length
    var j_id = this.data.j_id
    // console.log('题目数', pro_len)
    let pagenum = parseFloat(this.data.pagenum)
    pagenum += 1
    if (this.data.pagenum >= this.data.pro_grap.length) {
      let tranobj = {
        quest: this.data.pro_grap[7].question,
        anser: this.data.doc_advi
      }
      this.data.trandata.push(tranobj)
      console.log(this.data.trandata)
      
      let modul = JSON.stringify(this.data.trandata)
      console.log(modul)
      wx.setStorageSync('modul', modul)
      wx.setStorageSync('sickname', this.data.one_items) //疾病症状
      wx.setStorageSync('hospatil', this.data.items) //就诊过的医院科室
      wx.setStorageSync('sickimg', this.data.img) //患处照片
      wx.setStorageSync('medimg', this.data.img2) //药物照片
      wx.setStorageSync('sickhisty', this.data.five_items) //病史
      wx.setStorageSync('allergy', this.data.six_items) //过敏
      wx.setStorageSync('dochelp', this.data.doc_advi) //医生帮助
      var tel = this.data.tel
      let tranarr = [
        modul,
        this.data.one_items,
        this.data.items,
        this.data.img,
        this.data.img2,
        this.data.five_items,
        this.data.six_items,
        this.data.doc_advi
      ]
      wx.setStorage({
        key: 'nextdata',
        data: tranarr
      })
      wx.navigateTo({
        url: '/hyb_yl/czhuanjiasubpages/pages/questend/index?zid=' + zid + '&name=' + name + '&keywords=' + keywords + '&j_id=' + j_id + '&tel=' + tel
      })
      return false
    } else {
      let progress = parseFloat(this.data.progress)
      progress += 10
      console.log('进度条', progress)
      this.setData({
        pagenum: pagenum,
        progress: progress,
        ans_index: -1,
        addinfo_key: true,
        onedown_key: true,
        threedown_key: true,
        fourdown_key: true,
        fivedown_key: true,
        sixdown_key: true
      })
    }
  },
  //问题一start
  od_checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  od_changeradio: function(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let check = 'one_items[' + index + ']checked'
    this.setData({
      [check]: !this.data.one_items[index].checked
    })
  },
  //获取弹框内容
  od_getext: function(e) {
    this.setData({
      one_textarea: e.detail.value
    })
  },
  //弹框
  od_showpop: function(e) {
    this.setData({
      one_mask_key: false,
      one_textarea: ""
    })
  },
  od_enter: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    this.setData({
      one_mask_key: true
    })

    if (index == 1 && this.data.one_textarea) {
      let obj = {
        name: this.data.one_textarea,
        value: this.data.one_textarea,
        checked: true
      }

      let one_items = this.data.pro_grap[1].one_items
      this.data.pro_grap[1].one_items.push(obj)
      // console.log(one_items)
      let cone_items =`pro_grap[1].one_items`
      this.setData({
        [cone_items]: this.data.pro_grap[1].one_items
      })
    }
  },
  //删除数据
  od_deldata: function(e) {
    let index = e.currentTarget.dataset.index
    this.data.pro_grap[1].one_items.splice(index, 1)
    let cone_items = `pro_grap[1].one_items`
    this.setData({
      [cone_items]: this.data.pro_grap[1].one_items
    })
  },
  //end
  // 问题二start
  //获取input
  input1: function(e) {
    // console.log(e.detail.value)
    let input1 = `pro_grap[2].input1`
    this.setData({
      [input1]: e.detail.value
    })
  },
  input2: function(e) {
    // console.log(e.detail.value)
    let input2 = `pro_grap[2].input2`
    this.setData({
      [input2]: e.detail.value
    })
  },
  enter: function(e) {
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      this.setData({
        addinfo_key: true
      })
    } else if (index == 1) {
      let items = this.data.pro_grap[2].one_items
      let itemlen = items.length
      let input1 = this.data.pro_grap[2].input1
      let input2 = this.data.pro_grap[2].input2
      console.log('items', items, 'itemlen', itemlen, 'input1', input1, 'input2', input2)
      for (var i = 0; i < itemlen; i++) {
        if (items[i].name1 == input1 || items[i].name2 == input2) {
          this.setData({
            addinfos_key: false
          })
          return false
        }
      }
      if (input1 == "" || input2 == "") {
        wx.showToast({
          title: '请输入医院名称或科室名称',
          icon: 'none'
        })
      } else {
        let obj = {
          name1: this.data.pro_grap[2].input1,
          name2: this.data.pro_grap[2].input2,
          value1: this.data.pro_grap[2].input1,
          value2: this.data.pro_grap[2].input2,
          checked: true
        }
        // console.log(obj)
        this.data.pro_grap[2].one_items.push(obj)
        let input1 =`pro_grap[2].input1`
        let input2 = `pro_grap[2].input2`
        let one_items = `pro_grap[2].one_items`
        this.setData({
          addinfos_key: false,
          [one_items]: this.data.pro_grap[2].one_items,
          [input1]: '',
          [input2]: ''
        })
      }
    }
  },
  //多选框
  checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  changeradio: function(e) {
    let index = e.currentTarget.dataset.index;
    let check = 'items[' + index + '].checked'
    this.setData({
      [check]: !this.data.items[index].checked
    })
    console.log(index)
  },
  //删除多选框
  deldata: function(e) {
    let index = e.currentTarget.dataset.index;
    // console.log(index)
    this.data.pro_grap[2].one_items.splice(index, 1)
    let one_items =`pro_grap[2].one_items`

    this.setData({
      [one_items]: this.data.pro_grap[2].one_items
    })
  },
  //添加多选框
  addevent: function() {
    this.setData({
      addinfos_key: true
    })
  },
  // end

  //问题三start
  // 上传图片
  uploadimg: function() {
    let that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.data_arr;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function(res) {
              console.log(res)
              data_arr.push(res.data)
              let img =`pro_grap[3].img`
              that.setData({
                [img]: data_arr
              })
            }
          })
        }

        // that.setData({
        //   img: that.data.img.concat(res.tempFilePaths)
        // })
      }
    })
  },
  // 删除图片
  downimg: function(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.data.pro_grap[3].img.splice(index, 1);
    let img = `pro_grap[3].img`
    this.setData({
      [img]: this.data.img
    })
  },
  //end

  // 问题四start
  // 上传图片
  uploadimg2: function() {
    let that = this;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let img =`pro_grap[4].img`
        that.setData({
          [img]: that.data.pro_grap[4].img.concat(res.tempFilePaths)
        })
      }
    })
  },
  // 删除图片
  downimg2: function(e) {
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    this.data.pro_grap[4].img.splice(index, 1);
    let img = `pro_grap[4].img`
    this.setData({
      [img]: this.data.pro_grap[4].img
    })
  },
  //end
  //问题五start
  fd_checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  fd_changeradio: function(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let check = 'five_items[' + index + ']checked'
    this.setData({
      [check]: !this.data.five_items[index].checked
    })
    // let checked = ''
  },
  //获取弹框内容
  fd_getext: function(e) {
    this.setData({
      five_textarea: e.detail.value
    })
  },
  //弹框
  fd_showpop: function(e) {
    this.setData({
      five_mask_key: false,
      five_textarea: ""
    })
  },
  fd_enter: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    this.setData({
      five_mask_key: true
    })
  
    if (index == 1 && this.data.five_textarea) {
      let obj = {
        name: this.data.five_textarea,
        value: this.data.five_textarea,
        checked: true
      }
      this.data.pro_grap[5].one_items.push(obj)
      let one_items =`pro_grap[5].one_items`
      this.setData({
        [one_items]: this.data.pro_grap[5].one_items
      })
    }
  },
  //删除数据
  fd_deldata: function(e) {
    let index = e.currentTarget.dataset.index
    this.data.pro_grap[5].one_items.splice(index, 1)
    let one_items =`pro_grap[5].one_items`
    this.setData({
      [one_items]: this.data.pro_grap[5].one_items
    })
  },
  //end
  //问题六start
  six_checkboxChange: function(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  six_changeradio: function(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index
    let check = 'six_items[' + index + ']checked'
    this.setData({
      [check]: !this.data.six_items[index].checked
    })
    // let checked = ''
  },
  //获取弹框内容
  six_getext: function(e) {
    this.setData({
      six_textarea: e.detail.value
    })
  },
  //弹框
  six_showpop: function(e) {
    this.setData({
      six_mask_key: false,
      six_textarea: ""
    })
  },
  six_enter: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    this.setData({
      six_mask_key: true
    })
    for (var i = 0; i < that.data.pro_grap[6].one_items.length; i++) {
      if (that.data.pro_grap[6].one_items[i].value == that.data.six_textarea) {
        return false
      }
    }
    if (index == 1 && this.data.six_textarea) {
      let obj = {
        name: this.data.six_textarea,
        value: this.data.six_textarea,
        checked: true
      }
      let one_items =`pro_grap[6].one_items`
      this.data.pro_grap[6].one_items.push(obj)
      this.setData({
        [one_items]: this.data.pro_grap[6].one_items
      })
    }
  },
  //删除数据
  six_deldata: function(e) {
    let index = e.currentTarget.dataset.index
    this.data.pro_grap[6].one_items.splice(index, 1)
    let one_items = `pro_grap[6].one_items`
    this.setData({
      [one_items]: this.data.pro_grap[6].one_items
    })
  },
  //end
  //问题7start
  doc_advi: function(e) {
    this.setData({
      doc_advi: e.detail.value
    })

  },
  //end
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    let num = wx.getStorageSync('num')
    let progress = wx.getStorageSync('progress')
    var zid = options.zid
    var name = options.name
    var keywords = options.keywords
    var j_id = options.j_id
    var tel = options.tel
    // let text = wx.getStorageSync('text')
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data,
          zid: zid,
          name: name,
          keywords: keywords,
          j_id: j_id,
          tel: tel
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log('题目数', this.data.pro_grap.length)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})